package com.yylending.las.authority.verifycode;

import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Model;
import com.yyfax.infrastructure.network.NetworkHelper;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yylending.las.common.RSAEntity;
import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.POST;
import rx.Observable;

/**
 * Created by laixj on 2017/5/26.
 * Copyright ©2016 www.yylending.com. All Rights Reserved.
 */
public class VerifyCodeModel extends Model {
	private static final String TAG = "LoginModel";

	public final static int METHOD_GET_VERIFY_CODE = 1;

	@Override
	public Observable getObservable(Object params, int functionalCode) {
		Service service = (Service) NetworkHelper.createService(Service.class);
		switch (functionalCode) {
			case METHOD_GET_VERIFY_CODE:
				return service.sendVerificationCode("", new RSAEntity(params));
		}
		return null;
	}

	@Override
	public void onNext(Object obj) {
		CLog.i(TAG, "onNext: " + obj.toString());
		BaseResultEntity result = cast(obj);
		switch (result.requestCode) {
			case METHOD_GET_VERIFY_CODE:
				GetCodeResult getCodeResult = cast(obj);
				EventAgent.post(getCodeResult);
				break;
		}
	}

	private interface Service {
		/**
		 * 发送验证码
		 */
		@POST("server/verify/getSmsCode")
		Observable<GetCodeResult> sendVerificationCode(@Header("token") String token, @Body RSAEntity code);
	}

	static class GetCodeResult extends BaseResultEntity<VerifyCode> {
	}

	static class VerifyCode {
		public String verify;
		public String msg;
	}

	/**
	 * 类型，1：文本，2：语言 , 3：邮箱
	 */
	public final static String TYPE_TEXT = "1";
	public final static String TYPE_VOICE = "2";
	public final static String TYPE_EMAIL = "3";
	/**
	 * 操作 ：reg(注册）, resetPwd（找回密码）
	 */
	public final static String OPERATION_REGISTER = "reg";
	public final static String OPERATION_RESET_PASSWORD = "resetPwd";

	public static class VerificationCode {
		public String mobile;
		public String type = "1";
		public String operation;
		public String seconds;
		//type true string 1 类型，1：文本，2：语言 , 3：邮箱
		//operation true string reg 操作 ：reg(注册）, resetPwd（找回密码）
		//seconds true string 120 倒计时

		public VerificationCode(String type, String phone, String operation, String seconds) {
			this.mobile = phone;
			this.type = type;
			this.operation = operation;
			this.seconds = seconds;
		}

		@Override
		public String toString() {
			return "VerificationCode{" +
					"mobile='" + mobile + '\'' +
					", type='" + type + '\'' +
					", operation='" + operation + '\'' +
					", seconds='" + seconds + '\'' +
					'}';
		}
	}
}